const settingService = require('services/setting')
const _ = require('lodash')
const BANNER_SETTING = 'Banners'

const express = require('express')
const router = express.Router()

router.get('/', async (req, res, next) => {
  let banners = await settingService.get(BANNER_SETTING)
  if (banners) {
    banners = JSON.parse(banners)
  }
  res.json(banners)
})

router.post('/', async (req, res, next) => {
  let banners = req.body.banners
  if (_.isArray(banners)) {
    banners = banners.filter((banner) => {
      return banner.url
    })
    await settingService.set(BANNER_SETTING, JSON.stringify(banners))
  }
  res.send({status: 'ok'})
})

module.exports = router